﻿namespace GSC.WebControls
{
    using System;
    using System.Collections;
    using System.Drawing.Design;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Security.Permissions;
    /// <summary>
    /// 
    /// </summary>
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] 
    [Editor(typeof(BackgroundCenterLeftMenuTitleControlCollectionEditor), typeof(UITypeEditor))]
    public class BackgroundCenterLeftMenuTitleControlCollection : CollectionBase, IStateManager
    {  
        #region IStateManager
                /// <summary>
        /// 
        /// </summary>
        private bool _isTrackingViewState;
        /// <summary>
        /// 
        /// </summary>
        private bool _saveAll;
        /// <summary>
        /// 
        /// </summary>
        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return _isTrackingViewState;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        void IStateManager.LoadViewState(object state)
        {
            if (state == null)
                return;

            if (state is Pair)
            {
                // Load modified items.
                Pair p = (Pair)state;
                ArrayList indices = (ArrayList)p.First;
                ArrayList states = (ArrayList)p.Second;

                for (int i = 0; i < indices.Count; i++)
                {
                    int index = (int)indices[i];
                    if (index < this.Count)
                    {
                        ((IStateManager)((BackgroundCenterLeftMenuTitleControl)List[index])).LoadViewState(states[i]);
                    }
                    else
                    {
                        BackgroundCenterLeftMenuTitleControl shtabpage = new BackgroundCenterLeftMenuTitleControl();
                        Add(shtabpage);
                        ((IStateManager)shtabpage).LoadViewState(states[i]);
                    }
                }
            }
            else
            {
                // All items were saved.                
                _saveAll = true;
                object[] states = (object[])state;
                int count = states.Length;
                //List.Clear();               
                for (int i = 0; i < count; i++)
                {
                    BackgroundCenterLeftMenuTitleControl shtabpage = new BackgroundCenterLeftMenuTitleControl();
                    Add(shtabpage);
                    ((IStateManager)shtabpage).LoadViewState(states[i]);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        object IStateManager.SaveViewState()
        {
            if (_saveAll == true)
            {
                object[] states = new object[Count];
                for (int i = 0; i < Count; i++)
                {
                    BackgroundCenterLeftMenuTitleControl shtabpage = (BackgroundCenterLeftMenuTitleControl)List[i];
                    shtabpage.SetDirty();
                    states[i] = ((IStateManager)shtabpage).SaveViewState();
                }
                if (Count > 0)
                    return states;
                else
                    return null;
            }
            else
            {
                ArrayList indices = new ArrayList();
                ArrayList states = new ArrayList();
                for (int i = 0; i < Count; i++)
                {
                    BackgroundCenterLeftMenuTitleControl shtabpage = (BackgroundCenterLeftMenuTitleControl)List[i];
                    object state = ((IStateManager)shtabpage).SaveViewState();
                    if (state != null)
                    {
                        states.Add(state);
                        indices.Add(i);
                    }
                }
                if (indices.Count > 0)
                    return new Pair(indices, states);

                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        void IStateManager.TrackViewState()
        {
            _isTrackingViewState = true;
            for (int i = 0; i < Count; i++)
            {
                ((IStateManager)(BackgroundCenterLeftMenuTitleControl)List[i]).TrackViewState();
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
		public BackgroundCenterLeftMenuTitleControl this[int index]
		{
			get
			{
                return (BackgroundCenterLeftMenuTitleControl)this.List[index];				
			}
			set
			{
                this.List[index] = value;
			}
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public int Add(BackgroundCenterLeftMenuTitleControl item) 
		{
            List.Add(item);
            if (_isTrackingViewState)
            {
                ((IStateManager)item).TrackViewState();
                item.SetDirty();
            }
            return List.Count - 1;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public void Insert(int index, BackgroundCenterLeftMenuTitleControl item)
        {
            this.List.Insert(index, item);
            if (_isTrackingViewState)
            {
                ((IStateManager)item).TrackViewState();
                _saveAll = true;
            }
		}
		/// <summary>
		/// 
		/// </summary>
        /// <param name="item"></param>
        public void Remove(BackgroundCenterLeftMenuTitleControl item) 
		{
            this.List.Remove(item);
            if (_isTrackingViewState)
                _saveAll = true;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(BackgroundCenterLeftMenuTitleControl item) 
		{
            return this.List.Contains(item);
		}
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(BackgroundCenterLeftMenuTitleControl item) 
		{
            return this.List.IndexOf(item);			
		} 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
		public void CopyTo(BackgroundCenterLeftMenuTitleControl[] array, int index) 
		{ 
			List.CopyTo(array, index); 
		} 
        /// <summary>
        /// 
        /// </summary>
        public new void Clear()
        {
            foreach (BackgroundCenterLeftMenuTitleControl item in List)
            {
                item.Buttons.Clear();
            }
            List.Clear();
            if (_isTrackingViewState)
                _saveAll = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public new void RemoveAt(int index)
        {
            List.RemoveAt(index);
            if (_isTrackingViewState)
                _saveAll = true;
        }
    }
}